﻿using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageProcess
{
    public class OpenCVHelper
    {
        /// <summary>
        /// 去除黑边
        /// </summary>
        /// <param name="originalImage"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap? RemoveBlackEdge(System.Drawing.Bitmap originalImage)
        {
            // 读取原始图片
            Mat image = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalImage);

            Mat hsvImage = new Mat();
            Cv2.CvtColor(image, hsvImage, ColorConversionCodes.BGR2HSV);
            Cv2.ImShow("Original hsvImage", hsvImage);


            // 将图像转换为灰度图
            Mat grayImage = new Mat();
            Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);

            // 进行二值化处理
            Mat binaryImage = new Mat();
            Cv2.Threshold(grayImage, binaryImage, 100, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);

            // 进行边缘检测
            Mat edges = new Mat();
            Cv2.Canny(grayImage, edges, threshold1: 50, threshold2: 150);

            // 显示结果
            Cv2.NamedWindow("Original Image");
            Cv2.ImShow("Original Image", edges);
            Cv2.WaitKey(0);
            Cv2.DestroyAllWindows();

            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(binaryImage);
        }

        /// <summary>
        /// 获取HSV图片
        /// </summary>
        /// <param name="originalImage"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap? GetHSVImage(System.Drawing.Bitmap originalImage)
        {
            // 读取原始图片
            using Mat image = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalImage);

            using Mat hsvImage = new();
            Cv2.CvtColor(image, hsvImage, ColorConversionCodes.BGR2HSV);

            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(hsvImage);
        }

        /// <summary>
        /// 获取灰度图片
        /// </summary>
        /// <param name="originalImage"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap? GetGrayImage(System.Drawing.Bitmap originalImage)
        {
            // 读取原始图片
            using Mat image = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalImage);

            // 将图像转换为灰度图
            using Mat grayImage = new();
            Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);

            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(grayImage);
        }

        /// <summary>
        /// 获取二值化图片
        /// </summary>
        /// <param name="originalImage"></param>
        /// <returns></returns>
        public static System.Drawing.Bitmap? GetBinaryImage(System.Drawing.Bitmap originalImage)
        {
            // 读取原始图片
            using Mat image = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalImage);

            // 将图像转换为灰度图
            using Mat grayImage = new();
            Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);

            // 进行二值化处理
            using Mat binaryImage = new();
            Cv2.Threshold(grayImage, binaryImage, 100, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);

            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(binaryImage);
        }

        public static System.Drawing.Bitmap? GetImageContours(System.Drawing.Bitmap originalImage)
        {
            // 读取原始图片
            using Mat image = OpenCvSharp.Extensions.BitmapConverter.ToMat(originalImage);

            // 将图像转换为灰度图
            using Mat grayImage = new();
            Cv2.CvtColor(image, grayImage, ColorConversionCodes.BGR2GRAY);

            // 进行二值化处理
            using Mat binaryImage = new();
            Cv2.Threshold(grayImage, binaryImage, 100, 255, ThresholdTypes.Binary | ThresholdTypes.Otsu);

            // 查找图像中的所有轮廓
            Point[][] contours;
            HierarchyIndex[] hierarchy;
            Cv2.FindContours(binaryImage, out contours, out hierarchy, RetrievalModes.External, ContourApproximationModes.ApproxSimple);

            // 遍历每个轮廓并在原始图像上绘制边界线条
            foreach (var contour in contours)
            {
                Cv2.DrawContours(grayImage, new[] { contour }, -1, Scalar.Red, 2);
            }

            return OpenCvSharp.Extensions.BitmapConverter.ToBitmap(grayImage);
        }
    }
}
